package com.xinnet.mms.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xinnet.mms.entity.Emp;
import com.xinnet.mms.resources.service.ResourcesService;

@Controller
@RequestMapping(value = "/resources")
public class ResourcesController {
	
	@Autowired
	ResourcesService resourcesService;
	
	public void setResourcesService(ResourcesService resourcesService) {
		this.resourcesService = resourcesService;
	}


	@ResponseBody
	@RequestMapping(value = "/getResourcesList")
	@Transactional(propagation=Propagation.REQUIRED,readOnly=true)
	public List<Object> getResourcesList(HttpServletRequest request,HttpServletResponse response){

		String treeIds = request.getParameter("treeIds");
		HttpSession session=request.getSession();
		Emp emp = (Emp)session.getAttribute("currentUser");
		if(emp==null){
			return null;
		}
		String roleId = emp.getRolId();
		List<Object> menuTreeList = resourcesService.getMenuTreeListByRoleId(treeIds,roleId);
		return menuTreeList;
	}
	
	
	
	/*private List<Map<String,Object>> getMenuTreeList(List<Resources> resourcesList,String treeId){
		List<Map<String,Object>> menuTreeList = new ArrayList<Map<String,Object>>();
		Map<String,Object> nodeMap = new HashMap<String, Object>();

		List<Resources> leafResourcesList = new ArrayList<Resources>();
		for (Iterator<Resources> iterator = resourcesList.iterator(); iterator.hasNext();) {
			Resources resources = (Resources) iterator.next();
			String parentId = resources.getParentId()+"";
			String id = resources.getId()+"";
			if(parentId.equals(treeId) && !id.equals(treeId)){
				leafResourcesList.add(resources);
			}
		}
		for (Iterator<Resources> iterator = leafResourcesList.iterator(); iterator.hasNext();) {
			Resources resources = (Resources) iterator.next();
			String id = resources.getId()+"";
			String remark = resources.getRemark();
			String url = resources.getUrl();
			
			boolean isParentNode = false;
			for (Iterator<Resources> iterator2 = resourcesList.iterator(); iterator2.hasNext();) {
				Resources resources2 = (Resources) iterator2.next();
				String parentId = resources2.getParentId()+"";
				if(id.equals(parentId)){
					isParentNode = true;
					break;
				}
			}
			if(isParentNode){
				List<Map<String,Object>> leafNodeList = this.getMenuTreeList(resourcesList,id);
				nodeMap.put(remark, leafNodeList);
				
			}else{
				nodeMap.put(remark, url);
			}	
		}
		menuTreeList.add(nodeMap);
		return menuTreeList;	
	}*/

}
